package homework1;

import java.sql.SQLException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Controller 
{
	static Logger log = Logger.getLogger("lavasoft"); 
     
	public static void main(String args[])
	{
		log.setLevel(Level.INFO);

	    log.info("Start Running");
		Scanner scan=new Scanner(System.in);
		while(true)
		{
			String inputS=scan.nextLine();
			if(inputS.equals("exit"))//退出程序
			{
				log.info("End Running");
				scan.close();
				try 
				{
					DataBaseController.getDBConnecter().connection.close();
				} catch (SQLException e) 
				{
					e.printStackTrace();
				}
				return;
			}
			else if(inputS.startsWith("iyoga"))
			{
				handleInput(inputS.substring(6).split(" "));
			}
			else
			{
				System.out.println("wrong input!");
			}
		}
	}
	
	public static void handleInput(String[] inputs)//处理输入
	{
		if(inputs[0].equals("add-member"))//新增用户
		{
			try
			{
				int result=User.createUser(inputs[1],inputs[2],inputs[3]);
				if(result>0)
				{
					System.out.println("OK."+inputs[1]+"'s member id is "+User.formatID(result)+".");
				}
				else
				{
					System.out.println("WRONG! "+User.getCreateUserWrong(result));
				}
			}
			catch(Exception e)
			{
				log.warning("ERROR! "+e.getMessage());
				System.out.println("wrong input!");
			}
		}
		else if(inputs[0].equals("add-card"))//新增卡
		{
			try
			{
				if(inputs[2].equals("年卡"))
				{
					int result=Card.createYearCard(inputs[1]);
					if(result>0)
					{
						System.out.println("OK.A new 年卡 add to "+inputs[1]+"("+User.formatID(result)+").");
					}
					else
					{
						System.out.println("WRONG! "+Card.getCreateCardWrong(result));
					}
				}
				else if(inputs[2].equals("次卡"))
				{
					int result=Card.createTimesCard(inputs[1],Integer.parseInt(inputs[3]));
					if(result>0)
					{
						System.out.println("OK.A new 次卡 with "+inputs[3]+" times add to "+inputs[1]+".");
					}
					else
					{
						System.out.println("WRONG! "+Card.getCreateCardWrong(result));
					}
				}
				else throw(new Exception(inputs[2]));
			}
			catch(Exception e)
			{
				log.warning("ERROR! "+e.getMessage());
				System.out.println("wrong input!");
			}
		}
		else
		{
			System.out.println("wrong input!");
			log.warning(inputs[0]);
		}
	}
	

}
